//
//  TabBarCtrlUtils.m
//  OCStarterProject
//
//  Created by huaya on 2025/10/2.
//

#import "TabBarCtrlUtils.h"
#import "UIColor+Additions.h"
#import "MBProgressHUD.h"
#import "TLMeViewController.h"
#import "TLMallMainViewController.h"
#import "TLCollectionListViewController.h"

@interface TabBarCtrlUtils ()<UITabBarControllerDelegate>
@property (nonatomic, strong) NavigationController *navCtrl1;
@property (nonatomic, strong) NavigationController *navCtrl2;
@property (nonatomic, strong) NavigationController *navCtrl3;
@end

@implementation TabBarCtrlUtils

SINGLETON_IMPLEMENTATION(TabBarCtrlUtils)


- (TabBarController *)rootTabbarController{
    if (!_rootTabbarController) {
        _rootTabbarController = [self createTabbar];
    }
    return _rootTabbarController;
}
- (TabBarController *)createTabbar{
    
    {
        
        TLCollectionListViewController *vc = [[TLCollectionListViewController alloc] init];
        
        [vc.view setBackgroundColor:[UIColor redColor]];
        self.navCtrl1 = [[NavigationController alloc] initWithRootViewController:vc];
        self.navCtrl1.tabbarIndex = 0;
        [self.navCtrl1.tabBarItem mk_setTitle:WXLocalizedString(@"书架")
                                  normalImage:@"bookRack"
                                selectedImage:@"bookRack_select"];
    }{
        
        TLMallMainViewController *vc = [[TLMallMainViewController alloc] init];
        
        [vc.view setBackgroundColor:[UIColor blueColor]];
        self.navCtrl2 = [[NavigationController alloc] initWithRootViewController:vc];
        self.navCtrl2.tabbarIndex = 1;
        [self.navCtrl2.tabBarItem mk_setTitle:WXLocalizedString(@"发现")
                                  normalImage:@"bookMall"
                                selectedImage:@"bookMall_select"];
    }{
        
        TLMeViewController *vc = [[TLMeViewController alloc] init];
        [vc.view setBackgroundColor:[UIColor grayColor]];
        self.navCtrl3 = [[NavigationController alloc] initWithRootViewController:vc];
        self.navCtrl3.tabbarIndex = 2;
        [self.navCtrl3.tabBarItem mk_setTitle:WXLocalizedString(@"我的")
                                  normalImage:@"mine"
                                selectedImage:@"mine_select"];
    }
    TabBarController *tabBar = [[TabBarController alloc] init];
    tabBar.tabBar.tintColor = UIColor.blackColor;
    tabBar.tabBar.translucent = NO;
    tabBar.delegate = self;
    [tabBar.tabBar mk_setShadowWithOffset:CGSizeMake(0, -0.5) radius:4 color:MK_COLOR_HEX(0xE9EBED) opacity:0.8];
    [tabBar.tabBar mk_setTitleNormalColor:[UIColor colorWithHexString:@"B3B3B3"] selectedColor:UIColor.blackColor];
    tabBar.viewControllers = @[self.navCtrl1, self.navCtrl2, self.navCtrl3];
    tabBar.selectedIndex = 1;
    return tabBar;
}




#pragma mark - TabBar 相关方法

+ (nullable TabBarController *)currentTabBarController {
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    UIViewController *rootViewController = window.rootViewController;
    
    if ([rootViewController isKindOfClass:[TabBarController class]]) {
        return (TabBarController *)rootViewController;
    }
    
    return nil;
}

+ (nullable NavigationController *)currentNavigationController {
    TabBarController *tabBarController = [self currentTabBarController];
    if (tabBarController) {
        UIViewController *selectedViewController = tabBarController.selectedViewController;
        if ([selectedViewController isKindOfClass:[NavigationController class]]) {
            return (NavigationController *)selectedViewController;
        }
    }
    
    return nil;
}

+ (nullable UIViewController *)currentViewController {
    NavigationController *navigationController = [self currentNavigationController];
    if (navigationController) {
        return navigationController.topViewController;
    }
    
    return nil;
}

+ (void)switchToTabAtIndex:(NSUInteger)index {
    TabBarController *tabBarController = [self currentTabBarController];
    if (tabBarController && index < tabBarController.viewControllers.count) {
        tabBarController.selectedIndex = index;
    }
}

+ (NSUInteger)currentSelectedTabIndex {
    TabBarController *tabBarController = [self currentTabBarController];
    if (tabBarController) {
        return tabBarController.selectedIndex;
    }
    
    return 0;
}

#pragma mark - Navigation 相关方法

+ (void)pushViewController:(UIViewController *)viewController {
    [self pushViewController:viewController animated:YES];
}

+ (void)pushViewController:(UIViewController *)viewController hidesBottomBarWhenPushed:(BOOL)hidesBottomBarWhenPushed {
    viewController.hidesBottomBarWhenPushed = hidesBottomBarWhenPushed;
    [self pushViewController:viewController animated:YES];
}

+ (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    NavigationController *navigationController = [self currentNavigationController];
    if (navigationController) {
        [navigationController pushViewController:viewController animated:animated];
    }
}

+ (void)popViewController {
    [self popViewControllerAnimated:YES];
}

+ (void)popViewControllerAnimated:(BOOL)animated {
    NavigationController *navigationController = [self currentNavigationController];
    if (navigationController) {
        [navigationController popViewControllerAnimated:animated];
    }
}

+ (void)popToViewController:(UIViewController *)viewController {
    [self popToViewController:viewController animated:YES];
}

+ (void)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
    NavigationController *navigationController = [self currentNavigationController];
    if (navigationController) {
        [navigationController popToViewController:viewController animated:animated];
    }
}

+ (void)popToRootViewController {
    [self popToRootViewControllerAnimated:YES];
}

+ (void)popToRootViewControllerAnimated:(BOOL)animated {
    NavigationController *navigationController = [self currentNavigationController];
    if (navigationController) {
        [navigationController popToRootViewControllerAnimated:animated];
    }
}

+ (void)popToViewControllerOfClass:(Class)viewControllerClass {
    NavigationController *navigationController = [self currentNavigationController];
    if (navigationController) {
        for (UIViewController *viewController in navigationController.viewControllers) {
            if ([viewController isKindOfClass:viewControllerClass]) {
                [navigationController popToViewController:viewController animated:YES];
                return;
            }
        }
    }
}

+ (void)presentViewController:(UIViewController *)viewController {
    [self presentViewController:viewController completion:nil];
}

+ (void)presentViewController:(UIViewController *)viewController completion:(void (^)(void))completion {
    UIViewController *currentViewController = [self currentViewController];
    if (currentViewController) {
        [currentViewController presentViewController:viewController animated:YES completion:completion];
    }
}

+ (void)dismissViewController {
    [self dismissViewControllerWithCompletion:nil];
}

+ (void)dismissViewControllerWithCompletion:(void (^)(void))completion {
    UIViewController *currentViewController = [self currentViewController];
    if (currentViewController) {
        [currentViewController dismissViewControllerAnimated:YES completion:completion];
    }
}

#pragma mark - 导航栈操作

+ (nullable NSArray<UIViewController *> *)currentNavigationStack {
    NavigationController *navigationController = [self currentNavigationController];
    if (navigationController) {
        return navigationController.viewControllers;
    }
    
    return nil;
}

+ (BOOL)containsViewControllerOfClass:(Class)viewControllerClass {
    NSArray<UIViewController *> *viewControllers = [self currentNavigationStack];
    if (viewControllers) {
        for (UIViewController *viewController in viewControllers) {
            if ([viewController isKindOfClass:viewControllerClass]) {
                return YES;
            }
        }
    }
    
    return NO;
}

+ (nullable UIViewController *)viewControllerOfClass:(Class)viewControllerClass {
    NSArray<UIViewController *> *viewControllers = [self currentNavigationStack];
    if (viewControllers) {
        for (UIViewController *viewController in viewControllers) {
            if ([viewController isKindOfClass:viewControllerClass]) {
                return viewController;
            }
        }
    }
    
    return nil;
}

+ (void)removeViewControllerOfClass:(Class)viewControllerClass {
    NavigationController *navigationController = [self currentNavigationController];
    if (navigationController) {
        NSMutableArray<UIViewController *> *viewControllers = [navigationController.viewControllers mutableCopy];
        NSMutableArray<UIViewController *> *viewControllersToRemove = [NSMutableArray array];
        
        for (UIViewController *viewController in viewControllers) {
            if ([viewController isKindOfClass:viewControllerClass]) {
                [viewControllersToRemove addObject:viewController];
            }
        }
        
        [viewControllers removeObjectsInArray:viewControllersToRemove];
        navigationController.viewControllers = viewControllers;
    }
}

+ (void)replaceViewControllerOfClass:(Class)oldViewControllerClass withViewController:(UIViewController *)newViewController {
    NavigationController *navigationController = [self currentNavigationController];
    if (navigationController) {
        NSMutableArray<UIViewController *> *viewControllers = [navigationController.viewControllers mutableCopy];
        
        for (NSUInteger i = 0; i < viewControllers.count; i++) {
            UIViewController *viewController = viewControllers[i];
            if ([viewController isKindOfClass:oldViewControllerClass]) {
                [viewControllers replaceObjectAtIndex:i withObject:newViewController];
                break;
            }
        }
        
        navigationController.viewControllers = viewControllers;
    }
}

#pragma mark - 状态栏和导航栏设置

+ (void)setStatusBarStyle:(UIStatusBarStyle)style {
    UIApplication.sharedApplication.statusBarStyle = style;
}

+ (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated {
    NavigationController *navigationController = [self currentNavigationController];
    if (navigationController) {
        [navigationController setNavigationBarHidden:hidden animated:animated];
    }
}

+ (void)setNavigationBarBackgroundColor:(UIColor *)color {
    NavigationController *navigationController = [self currentNavigationController];
    if (navigationController) {
        [navigationController mk_setBarColor:color];
    }
}

+ (void)setNavigationBarTitleColor:(UIColor *)color {
    NavigationController *navigationController = [self currentNavigationController];
    if (navigationController) {
        NSDictionary *attributes = @{
            NSForegroundColorAttributeName: color,
            NSFontAttributeName: [UIFont boldSystemFontOfSize:18]
        };
        navigationController.navigationBar.titleTextAttributes = attributes;
    }
}

+ (void)setNavigationBarTintColor:(UIColor *)color {
    NavigationController *navigationController = [self currentNavigationController];
    if (navigationController) {
        navigationController.navigationBar.tintColor = color;
    }
}

#pragma mark - 工具方法

+ (void)showToast:(NSString *)message {
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    if (window) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.label.text = message;
        hud.label.numberOfLines = 0;
        hud.offset = CGPointMake(0, 0);
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:YES afterDelay:2.0];
    }
}

+ (void)showLoading {
    [self showLoadingWithMessage:nil];
}

+ (void)showLoadingWithMessage:(nullable NSString *)message {
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    if (window) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
        hud.mode = MBProgressHUDModeIndeterminate;
        if (message) {
            hud.label.text = message;
        }
        hud.removeFromSuperViewOnHide = YES;
    }
}

+ (void)hideLoading {
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    if (window) {
        [MBProgressHUD hideHUDForView:window animated:YES];
    }
}

+ (void)showConfirmAlertWithTitle:(nullable NSString *)title
                          message:(nullable NSString *)message
                     confirmTitle:(nullable NSString *)confirmTitle
                      cancelTitle:(nullable NSString *)cancelTitle
                    confirmAction:(nullable void(^)(void))confirmAction
                     cancelAction:(nullable void(^)(void))cancelAction {
    
    UIViewController *currentViewController = [self currentViewController];
    if (!currentViewController) {
        return;
    }
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
                                                                             message:message
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    
    // 确认按钮
    NSString *confirmButtonTitle = confirmTitle ?: @"确定";
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:confirmButtonTitle
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * _Nonnull action) {
        if (confirmAction) {
            confirmAction();
        }
    }];
    [alertController addAction:confirm];
    
    // 取消按钮
    if (cancelTitle || cancelAction) {
        NSString *cancelButtonTitle = cancelTitle ?: @"取消";
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:cancelButtonTitle
                                                               style:UIAlertActionStyleCancel
                                                             handler:^(UIAlertAction * _Nonnull action) {
            if (cancelAction) {
                cancelAction();
            }
        }];
        [alertController addAction:cancel];
    }
    
    [currentViewController presentViewController:alertController animated:YES completion:nil];
}

@end
